package com.cskaoyan.controller.wx;

import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.model.admin.vo.BaseRespVoNB;
import com.cskaoyan.model.vx.bo.WxCartCheckedBo;
import com.cskaoyan.model.vx.bo.WxCartUpdateBo;
import com.cskaoyan.model.vx.vo.WxCartListVo;
import com.cskaoyan.model.vx.vo.WxCheckoutVo;
import com.cskaoyan.service.wx.WxCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/12/2 10:41
 */
@RequestMapping("/wx/cart")
@RestController
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    @RequestMapping("index")
    public BaseRespVo index(HttpSession session){
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId==null){
            return BaseRespVo.fail(501,"没有登录");
        }
        WxCartListVo wxCartListVo=wxCartService.getCartIndex(userId);
        return BaseRespVo.ok(wxCartListVo);
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody WxCartCheckedBo wxCartCheckedBo){
        WxCartListVo wxCartListVo=wxCartService.getCartChecked(wxCartCheckedBo);
        return BaseRespVo.ok(wxCartListVo);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody WxCartCheckedBo wxCartCheckedBo){
        WxCartListVo wxCartListVo=wxCartService.deleteCartChecked(wxCartCheckedBo);
        return BaseRespVo.ok(wxCartListVo);
    }

    @PostMapping("update")
    public BaseRespVoNB update(@RequestBody WxCartUpdateBo wxCartUpdateBo){
        wxCartService.updateCartGood(wxCartUpdateBo);
        return BaseRespVoNB.ok();
    }

    @PostMapping("add")
    public BaseRespVo add(HttpSession session,@RequestBody WxCartUpdateBo wxCartUpdateBo){
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId==null){
            return BaseRespVo.fail(501,"没有登录");
        }
        int cartCount=wxCartService.addCartGood(wxCartUpdateBo,userId);
        return BaseRespVo.ok(cartCount);
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodsCount(HttpSession session){
        Integer userId = (Integer) session.getAttribute("wxUserId");

        int cartCount=wxCartService.countCart(userId);
        return BaseRespVo.ok(cartCount);
    }

    @PostMapping("fastadd")
    public BaseRespVo fastAdd(HttpSession session,@RequestBody WxCartUpdateBo wxCartUpdateBo){
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId==null){
            return BaseRespVo.fail(501,"没有登录");
        }
        int cartId=wxCartService.fastAddCartGoods(wxCartUpdateBo,userId);
        return BaseRespVo.ok(cartId);
    }

    /**
     * 思考:checkout是否需要更改表的数据?
     * 它的功能不是很清楚
     */
    @RequestMapping("checkout")
    public BaseRespVo checkOut(HttpSession session,Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId){
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId==null){
            return BaseRespVo.fail(501,"没有登录");
        }
        WxCheckoutVo wxCheckoutVo=wxCartService.checkOutCart(cartId,addressId,couponId,userCouponId,grouponRulesId,userId);
        return BaseRespVo.ok(wxCheckoutVo);
    }
}
